import Vue from 'vue'
import VueRouter from 'vue-router'
import Home from '../views/Home.vue'
import {
	getToken
} from '@/utils/auth'
Vue.use(VueRouter)

const routes = [{
		path: '/',
		name: 'home',
		component: Home,
		meta: {
			title: '首页',
			header: 'default'
		}
	}, {
		path: '/InfoHome',
		name: 'InfoHome',
		component: () => import('../views/InfoHome.vue'),
		meta: {
			title: '学习平台',
			header: 'info',
		}
	}, {
		path: '/InfoLesson',
		name: 'InfoLesson',
		component: () => import('../views/InfoLesson.vue'),
		meta: {
			title: '视频',
			header: 'info',
		}
	}, {
		path: '/AbilityHome',
		name: 'AbilityHome',
		component: () => import('../views/AbilityHome.vue'),
		meta: {
			title: '馆员能力提升',
			header: 'staff'
		}
	}, {
		path: '/AbilityLesson',
		name: 'AbilityLesson',
		component: () => import('../views/AbilityLesson.vue'),
		meta: {
			title: '视频',
			header: 'staff',
		}
	}, {
		path: '/ReadHome',
		name: 'ReadHome',
		component: () => import('../views/ReadHome.vue'),
		meta: {
			title: '阅读推广',
			header: 'read'
		}
	}, {
		path: '/ReadLesson',
		name: 'ReadLesson',
		component: () => import('../views/ReadLesson.vue'),
		meta: {
			title: '视频',
			header: 'read',
		}
	}, {
		path: '/MatchHome',
		name: 'MatchHome',
		component: () => import('../views/MatchHome.vue'),
		meta: {
			title: '竞赛平台',
			header: 'match'
		}
	}, {
		path: '/Notice',
		name: 'Notice',
		component: () => import('../views/Notice.vue'),
		meta: {
			title: '大赛公告'
		}
	}, {
		path: '/News',
		name: 'News',
		component: () => import('../views/News.vue'),
		meta: {
			title: '新闻动态',
			header: 'default'
		}
	}, {
		path: '/ArticleList',
		name: 'ArticleList',
		component: () => import('../views/ArticleList.vue'),
		meta: {
			title: '新闻动态',
			header: 'default'
		}
	}, {
		path: '/NoticeDetails',
		name: 'NoticeDetails',
		component: () => import('../views/NoticeDetails.vue'),
		meta: {
			title: '详情',
			header: 'default'
		}
	}, {
		path: '/ArticleDetails',
		name: 'ArticleDetails',
		component: () => import('../views/ArticleDetails.vue'),
		meta: {
			title: '详情',
			header: 'default'
		}
	}, {
		path: '/Trends',
		name: 'Trends',
		component: () => import('../views/Trends.vue'),
		meta: {
			title: '成员馆动态'
		}
	}, {
		path: '/TrendsDetails',
		name: 'TrendsDetails',
		component: () => import('../views/TrendsDetails.vue'),
		meta: {
			title: '成员馆动态详情'
		}
	},
	{
		path: '/Member',
		name: 'Member',
		component: () => import('../views/Member.vue'),
		meta: {
			title: '成员馆',
			header: 'default'
		}
	}, {
		path: '/MemberDetails',
		name: 'MemberDetails',
		component: () => import('../views/MemberDetails.vue'),
		meta: {
			title: '成员馆详情',
			header: 'default'
		}
	}, {
		path: '/Teacher',
		name: 'Teacher',
		component: () => import('../views/Teacher.vue'),
		meta: {
			title: '名师展播列表'
		}
	}, {
		path: '/TeacherIntro',
		name: 'TeacherIntro',
		component: () => import('../views/TeacherIntro.vue'),
		meta: {
			title: '名师展播详情'
		}
	}, {
		path: '/Lesson',
		name: 'Lesson',
		component: () => import('../views/Lesson.vue'),
		meta: {
			title: '视频'
		}
	}, {
		path: '/MatchList',
		name: 'MatchList',
		component: () => import('../views/MatchList.vue'),
		meta: {
			title: '竞赛平台',
			header: 'match'
		}
	}, {
		path: '/UserInfo',
		name: 'UserInfo',
		component: () => import('../views/UserInfo.vue'),
		meta: {
			title: '个人中心'
		}
	}, {
		path: '/VideoIntro',
		name: 'VideoIntro',
		component: () => import('../views/VideoIntro.vue'),
		meta: {
			title: '课程章节'
		}
	}, {
		path: '/VideoDetails',
		name: 'VideoDetails',
		component: () => import('../views/VideoDetails.vue'),
		meta: {
			title: '课程学习'
		}
	}, {
		path: '/Simulation',
		name: 'Simulation',
		component: () => import('../views/Simulation.vue'),
		meta: {
			title: '模拟训练',
			header: 'match'
		}
	}, {
		path: '/Login',
		name: 'Login',
		component: () => import('../views/Login.vue'),
		meta: {
			title: '登录'
		}
	}, {
		path: '/Register',
		name: 'Register',
		component: () => import('../views/Register.vue'),
		meta: {
			title: '注册'
		}
	}, {
		path: '/UserSet',
		name: 'UserSet',
		component: () => import('../views/UserSet.vue'),
		meta: {
			title: '个人设置'
		}
	}, {
		path: '/Exam',
		name: 'Exam',
		component: () => import('../views/Exam.vue'),
		meta: {
			title: '答题'
		}
	}, {
		path: '/ErrorExam',
		name: 'ErrorExam',
		component: () => import('../views/ErrorExam.vue'),
		meta: {
			title: '错题本'
		}
	}, {
		path: '/ExamComp',
		name: 'ExamComp',
		component: () => import('../views/ExamComp.vue'),
		meta: {
			title: '考试'
		}
	}, {
		path: '/Feedback',
		name: 'Feedback',
		component: () => import('../views/Feedback.vue'),
		meta: {
			title: '反馈'
		}
	},

	{
		path: '/test',
		name: 'test',
		component: () => import( /* webpackChunkName: "about" */ '../views/test.vue')
	}
]

const router = new VueRouter({
	mode: 'history',
	base: process.env.BASE_URL,
	routes
})
const isLoginList = ['/InfoLesson', '/AbilityLesson', '/ReadLesson', '/Lesson', '/Simulation', '/Match', '/UserInfo',
	'/VideoIntro', '/MatchHome'
]

router.beforeEach((to, from, next) => {
	window.document.title = to.meta.title
	if (getToken()) {
		/* has token*/
		if (to.path === '/login') {
			next({
				path: '/'
			})
		} else {
			next()
		}
	} else {
		// 没有token
		if (isLoginList.indexOf(to.path) == -1) {
			// 在免登录白名单，直接进入
			next()
		} else {
			next(`/login?redirect=${to.fullPath}`)
		}
	}
})
const originalPush = VueRouter.prototype.push
VueRouter.prototype.push = function push(location) {
	return originalPush.call(this, location).catch(err => err)
}
export default router